package com.beone.smart.smartBeauty.controller.appApi;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.beone.smart.smartBeauty.dao.PersonInfoDao;
import com.beone.smart.smartBeauty.dao.SkinGatherDao;
import com.beone.smart.smartBeauty.dao.SleepInfoRecordDao;
import com.beone.smart.smartBeauty.dao.WeightRecordDao;
import com.beone.smart.smartBeauty.exception.GlobalException;
import com.beone.smart.smartBeauty.model.dto.BodyAndWeightDto;
import com.beone.smart.smartBeauty.model.dto.SkinGatherPersonDto;
import com.beone.smart.smartBeauty.model.dto.SleepAndMeasureDto;
import com.beone.smart.smartBeauty.model.entity.*;
import com.beone.smart.smartBeauty.model.result.ResultEntity;
import com.beone.smart.smartBeauty.model.result.ResultTool;
import com.beone.smart.smartBeauty.service.intf.GoodsService;
import com.beone.smart.smartBeauty.service.intf.RecommendService;
import com.beone.smart.smartBeauty.utils.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.HashMap;

/**
 * Created by bruce on 20/2/24 -- 16:27
 */
@Api(description = "移动app首页接口")
@Slf4j
@RestController
@RequestMapping(value = "/mobileAppHome")
public class AppController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private WeightRecordDao weightRecordDao;
    @Autowired
    private PersonInfoDao personInfoDao;
    @Autowired
    private SkinGatherDao skinGatherDao;
    @Autowired
    private SleepInfoRecordDao sleepInfoRecordDao;
    @Autowired
    private RecommendService recommendService;

    /**
     * 1、美容方案-列表展示
     */
    @ApiOperation(value = "美容方案-列表展示", notes = "列表查询")
    @GetMapping(value = "/articleList")
    public ResultEntity articleList(@RequestParam(value = "pageSize", required = false, defaultValue = "10") long pageSize,
                                    @RequestParam(value = "pageNum", required = false, defaultValue = "1") long pageNum) {
        IPage<RecommendArticleDO> page = new Page<>(pageNum, pageSize);
        return ResultTool.ok(recommendService.page(page));
    }

    /**
     * 1、美容方案-推荐列表
     */
    @ApiOperation(value = "美容方案-推荐列表", notes = "列表查询")
    @GetMapping(value = "/articleRecommendList")
    public ResultEntity articleRecommendList() {
        LambdaQueryWrapper<RecommendArticleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(RecommendArticleDO::getReadNumber);
        return ResultTool.ok(recommendService.list(queryWrapper));
    }

    /**
     * 1、美容方案-方案详情
     */
    @ApiOperation(value = "美容方案-方案详情", notes = "传入id,得到方案详情")
    @GetMapping(value = "/articleDetail/{id}")
    public ResultEntity articleDetail(@PathVariable String id) {
        RecommendArticleDO recommendArticleDO = recommendService.getById(id);
        if (recommendArticleDO == null) {
            throw new GlobalException("id有误，查询为null");
        }
        return ResultTool.ok(recommendArticleDO);
    }

    /**
     * 2、肤质检测
     */
    @ApiOperation(value = "肤质检测", notes = "最新的肤质数据")
    @GetMapping(value = "/skinInfo")
    public ResultEntity skinInfo() {
        String uid = RequestUtil.getUid();
        HashMap<String, Double> zonghe = skinGatherDao.personZongheCount(uid);
        SkinGatherDO skinGatherDO = skinGatherDao.personCount(uid);
        SkinGatherPersonDto skinGatherPersonDto = new SkinGatherPersonDto();
        skinGatherPersonDto.setPersonSkin(skinGatherDO);
        if (zonghe != null) {
            skinGatherPersonDto.setZonghe(zonghe.get("zonghe"));
        }
        return ResultTool.ok(skinGatherPersonDto);
    }

    /**
     * 3、睡眠情况
     */
    @ApiOperation(value = "睡眠情况-最新数据", notes = "列表查询,可分页")
    @GetMapping(value = "/sleepInfo")
    public ResultEntity sleepInfo() {
        String uid = RequestUtil.getUid();
        SleepInfoRecordDO sleepInfoRecordDO = sleepInfoRecordDao.getLastRecord(uid);
        return ResultTool.ok(new SleepAndMeasureDto(sleepInfoRecordDO, sleepInfoRecordDao.getTopAndTimes(uid)));
    }

    /**
     * 3、睡眠历史记录
     */
    @ApiOperation(value = "睡眠情况-历史记录", notes = "列表查询,可分页")
    @GetMapping(value = "/sleepInfoHistoryList")
    public ResultEntity sleepInfoHistoryList(@RequestParam(value = "pageSize", required = false, defaultValue = "10") long pageSize,
                                             @RequestParam(value = "pageNum", required = false, defaultValue = "1") long pageNum) {
        Page<SleepInfoRecordDO> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SleepInfoRecordDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SleepInfoRecordDO::getUserId, RequestUtil.getUid());
        return ResultTool.ok(sleepInfoRecordDao.selectPage(page, queryWrapper));
    }

    /**
     * 4、体重测量历史
     */
    @ApiOperation(value = "体重测量历史", notes = "列表查询,可分页")
    @GetMapping(value = "/weightRecordHistory")
    public ResultEntity weightRecordHistory(
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") long pageSize,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") long pageNum) {
        String uid = RequestUtil.getUid();
        LambdaQueryWrapper<WeightRecordDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WeightRecordDo::getUserId, uid);
        Page<WeightRecordDo> page = new Page<>(pageNum, pageSize);
        //体重测量历史的分页数据
        IPage<WeightRecordDo> iPage = weightRecordDao.selectPage(page, queryWrapper);
        return ResultTool.ok(iPage);
    }

    /**
     * 4、体重信息查询
     */
    @ApiOperation(value = "体重测量次数，信息查询")
    @GetMapping(value = "/weightInfoQuery")
    public ResultEntity weightInfoQuery() {
        String uid = RequestUtil.getUid();
        LambdaQueryWrapper<PersonInfoConfigDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PersonInfoConfigDO::getUserId, uid);
        PersonInfoConfigDO personInfo = personInfoDao.selectOne(queryWrapper);
        if (personInfo == null) {
            throw new GlobalException(ResultTool.HttpResponseCode.NOT_FOUND, "未查找到该用户信息");
        }
        personInfo.setCurrentWeight(weightRecordDao.getCurrentWeight(uid));
        return ResultTool.ok(new BodyAndWeightDto(weightRecordDao.getTopAndTimes(uid), personInfo));
    }

    /**
     * 5、美容商城-列表
     */
    @ApiOperation(value = "美容商城-列表", notes = "列表查询,可分页")
    @GetMapping(value = "/beautyMall/list")
    public ResultEntity beautyMallList(
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") long pageSize,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") long pageNum,
            @RequestParam(value = "searchKey", required = false) String searchKey) {
        LambdaQueryWrapper<GoodsDO> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(searchKey)) {
            queryWrapper.like(GoodsDO::getTitle, searchKey);
        }
        Page<GoodsDO> page = new Page<>(pageNum, pageSize);
        IPage<GoodsDO> iPage = goodsService.page(page, queryWrapper);
        return ResultTool.ok(iPage);
    }

    /**
     * 5、美容商城-商品详情
     */
    @ApiOperation(value = "美容商城的商品详情", notes = "传入商品id，得到商品详情")
    @GetMapping(value = "/beautyMall/detail/{id}")
    public ResultEntity beautyMallDetail(@PathVariable @NotBlank String id) {
        return ResultTool.ok(goodsService.getById(id));
    }
}
